package dProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvocationHandle implements InvocationHandler {
    private Object object;
    public MyInvocationHandle(Object object) {
    	this.object=object;
    }
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		method.invoke(object, args);
		return null;
	}
	public  Object getProxy() {
		//获得加载被代理类的 类加载器
				ClassLoader loader = this.object.getClass().getClassLoader();
				//指明被代理类实现的接口
				Class<?>[] interfaces = this.object.getClass().getInterfaces();
				// 创建被代理类的委托类,之后想要调用被代理类的方法时，都会委托给这个类的invoke(Object proxy, Method method, Object[] args)方法
				MyInvocationHandle h = new MyInvocationHandle(this.object);
				//生成代理类
				//注意newProxyInstance的三个参数所代表的含义
				Object ob=Proxy.newProxyInstance(loader, interfaces, h);
				return ob;
	}
	
	
	public static void main(String [] args){
		//创建需要被代理的类
		One one = new One();
		MyInvocationHandle mIh=new MyInvocationHandle(one);
		//注意newProxyInstance的三个参数所代表的含义
		Student proxy = (Student)mIh.getProxy();
		//通过代理类调用 被代理类的方法
		proxy.student();
	}

}
